﻿using System;
using System.Collections.Generic;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Wicresoft.EKA.NRemedy;


namespace Wicresoft.EKA.ITSM.BusinessImpl
{
    public class UserInfo : UserInfoBase
    {
        protected override void Dispose(bool disposing)
        {
        }

        public override System.Collections.Generic.ICollection<string> GetUserInRolesByUserIdentify(string identify)
        {
            //1. 获取这个人所代理的人员；
            //2. 获取本人及其代理人员的组权限
            //3. 取2结果的并集即可。

            //指派给组的 只能是一个组。
            //组是不用替换的，因为只要把这个人加入到组就可以了。

            //当这个人的多个组(即本方法的返回的结果） 包含本单子的 Assign Group，
            //或其登录名及代理的人，包含在本单子的Assign里面或ask for more 里面。或者本人是管理员，就可以审批
            //读取数据时，Request Form 与Request Store Join.                                                     在客户端判断。

            List<string> permissions = new List<string>();

            var key = "GetUserInRolesByUserIdentify" + identify;
            var cache = CacheHelper.GetCache(key);
            if (cache != null) return (System.Collections.Generic.List<string>)cache;
            else
            {
                var groupList = this.GetARProxy<SMGXJGroupAssoModel>().GetListEntry("'LoginID' =\"" + identify + "\" AND 'Disabled' = \"No\"", new System.Collections.Generic.List<uint>() 
                {
                    SMGXJGroupAssoModel.GroupID_ID,
                    SMGXJGroupAssoModel.GroupName_ID
                });
                foreach (var group in groupList)
                {
                    //permissions.Add(new string[] { group.GroupID.ToString(), group.GroupName });
                    permissions.Add(group.GroupName.ToString());
                }
                CacheHelper.SetCacheWithAbsoluteExpiration(key, permissions);
            }
            #region Example

            //IUser user = new User();

            //((Base)user).UserIdentify = ((Base)this).UserIdentify;

            //UserModel u = user.GetUserByLoginID(identify);

            //var groupList = string.Empty;

            //var valueList=new BMC.ARSystem.FieldValueList();

            //valueList[870000000]=identify;

            //groupList = (string)this.GetARServer().BMCServer.ExecuteService("EKA_UserInfo_", "000000000000001", valueList, new uint[] { 870000001 })["870000001"];

            //if (!string.IsNullOrWhiteSpace(groupList))
            //{
            //    permissions = groupList.Split(';').ToList();
            //}

            //当其能打开，意味着其能编辑

            //permissions.Union(

            #endregion

            //CacheHelper.SetCacheWithAbsoluteExpiration(key, permissions);

            return permissions;
        }

        public override System.Collections.Generic.Dictionary<string, string> GetUserPreferenceByUserIdentify(string identify)
        {
            var key = "GetUserPreferenceByUserIdentify" + identify;
            var cache = CacheHelper.GetCache(key);
            if (cache != null) return (System.Collections.Generic.Dictionary<string, string>)cache;

            if (string.IsNullOrWhiteSpace(identify)) return new Dictionary<string, string>() { { "FullName", "Guest User" } };
            var item = this.GetARProxy<CTMPeopleModel>().GetListEntry(CTMPeopleModel.RemedyLoginID_Name.S() + "=" + identify.D(),
                new List<uint>()
            {
                CTMPeopleModel.FullName_ID,
                CTMPeopleModel.RemedyLoginID_ID,
            });
            Dictionary<string, string> result = new Dictionary<string,string>();

            if (item == null)
            {
                return null;
            }
            else
            {
                result = new Dictionary<string, string>() { { "FullName", item[0].FullName } };
            }


            CacheHelper.SetCacheWithAbsoluteExpiration(key, result);
            return result;

        }

        public override ICollection<string> GetUserAlternativers(string identify)
        {
            return new List<string>() { "dddd" };
        }

        public override void Log(string type, string description, DateTime date)
        {

        }
    }
}
